<?php

class http{
    
    CONST HOST ="0.0.0.0";
    CONST PORT =8811;
    
    public $http=null;
    public  function __construct(){
        $this->http =new swoole_http_server(self::HOST,self::PORT);
        
        $this->http->set(
            [
                'enable_static_handler'=>true,
                'document_root'=>"/www/wwwroot/ymh/swoole/public/static",
                'worker_num'=>2,
//                 'task_worker_num' =>2,
            ]);
        
        $this->http->on("WorkerStart",[$this,'onWorkerStart']);
        $this->http->on("request",[$this,'onRequest']);
        
        $this->http->start();
        
    }
    
    public function OnWorkerStart($server,$work_id){
        define('APP_PATH', __DIR__ . '/../application/');
        
        // 加载基础文件
//         require __DIR__ . '/../thinkphp/base.php';
    }
    
    public  function  onRequest($request,$response){
        
        // 加载基础文件
        require_once __DIR__ . '/../thinkphp/base.php';
        
        if(isset($request->server)){
            foreach($request->server as $key=>$val){
                $_SERVER[strtoupper($key)]=$val;
            }
        }
        
        if(isset($request->header)){
            foreach($request->header as $key=>$val){
                $_SERVER[strtoupper($key)]=$val;
            }
        }
        
        $_GET=[];
        
        if(isset($request->get)){
            foreach($request->get as $key=>$val){
                $_GET[$key]=$val;
            }
        }
        
        
        if(isset($request->post)){
            foreach($request->post as $key=>$val){
                $_POST[$key]=$val;
            }
        }
        ob_start();
        
                   try {
                        think\Container::get('app', [APP_PATH])
                        ->run()
                        ->send();
                    } catch (\Exception $e) {
                       echo ("<h1>404</h1>");
                       echo "<pre>";
                       echo ($e);
                    }
        
//                     echo "<h1>Function</h1>".PHP_EOL;
        //           var_dump(request()->action());
        
        $res=ob_get_contents();
        ob_end_clean();
        
        
        $response->end($res);
        
    }
    
  
}
new http();